package com.xinli.xinlifm.view.service;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;

import com.xinli.xinlifm.http.bean.player.Mp3Info;
import com.xinli.xinlifm.utils.ConstantValue;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class MusicService extends Service implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, Handler.Callback {


    MediaPlayer mediaPlayer = null;
    private List<String> list;
    private Intent intent = new Intent("seekBar.length");
    private File directory;
    private TextView timeText;
    private Handler handler = new Handler(this);
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss", Locale.CHINA);
    private int duration;
    private SeekBar seekbar;
    private String pathUrl;


    @Override
    public void onCreate() {
        findMusic();
        directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnErrorListener(this);
        }
    }

    private void findMusic() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("自定义标签", "类名==MusicService" + "方法名==onStartCommand=====:" + "");
        EventBus.getDefault().post(mediaPlayer);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    private static int isPlayPath;

    /**
     * 播放
     *
     * @param position
     */
    public void playMusic(final int position) {


            mediaPlayer.reset();
            isPlayPath = position;
            pathUrl = "http://yiapi.xinli001.com/fm/media-url.mp3?id="+position+"&flag=0";

        Log.e("自定义标签", "类名==MusicService" + pathUrl);
            try {
                mediaPlayer.setDataSource(pathUrl);
                mediaPlayer.prepare();
                mediaPlayer.start();
                intent.putExtra("status", ConstantValue.START);
                sendBroadcast(intent);
            } catch (IOException e) {
                e.printStackTrace();
            }




    }

    /**
     * 暂停
     */
    public void pauseMusic() {
        mediaPlayer.pause();
//        handler.removeMessages(ConstantValue.seek_auto_update);
//        handler.removeMessages(2);
        intent.putExtra("status", ConstantValue.STOP);
        sendBroadcast(intent);
    }

    /**
     * 暂停后开始，要个boolean值来判断，
     * 应该有些问题
     */
    public void startMusic() {
        mediaPlayer.start();
        intent.putExtra("status", ConstantValue.START);
        sendBroadcast(intent);
    }

    /**
     * 可能在这个应用不需要调用这个方法
     */
    public void stopMusic() {
        mediaPlayer.stop();

    }

    /**
     * 下一曲
     */
    public void nextMusic() {
        int position;
        if (isPlayPath + 1 > list.size() - 1) {
            position = 0;
        } else {
            position = isPlayPath + 1;
        }
        playMusic(position);
    }

    /**
     * 上一曲
     */
    public void preMusic() {
        int position;
        if (isPlayPath - 1 < 0) {
            position = list.size() - 1;
        } else {
            position = isPlayPath - 1;
        }
        playMusic(position);
    }

    public void toSeek(int position) {
        mediaPlayer.seekTo(position);
    }

    public void findFindMusic(List<String> list) {
        if (this.list == null) {
            this.list = list;
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        intent.putExtra("status", ConstantValue.seek_bar_completion);
        sendBroadcast(intent);
        playMusic(isPlayPath);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        mp.release();
        return false;
    }

    private void acSeekBar(SeekBar seekbar) {
        this.seekbar = seekbar;
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 123:

                break;
        }
        return true;
    }

    private class MyBinder extends Binder implements IService {

        @Override
        public void callPlayMusic(int position) {
            playMusic(position);
        }

        @Override
        public void callPauseMusic() {
            pauseMusic();
        }

        @Override
        public void callNextMusic() {
            nextMusic();
        }

        @Override
        public void callPreMusic() {
            preMusic();
        }

        @Override
        public void callStartMusic() {
            startMusic();
        }

        @Override
        public void putMusicList(List<String> list) {
            findFindMusic(list);
        }

        @Override
        public void callToSeek(int position) {
            toSeek(position);
        }

        @Override
        public void putAcSeekBar(SeekBar seekbar) {
            acSeekBar(seekbar);
        }


    }


}
